package com.skyz.base.dialog;

import android.os.Bundle;
import android.text.Editable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.fragment.app.FragmentManager;

import com.skyz.base.R;
import com.skyz.base.util.ScreenUtils;

public class SecretDoorPasswordDialogFragment extends BaseDialogFragment implements View.OnClickListener {

    public static void showDialogFragment(@NonNull FragmentManager manager, @Nullable String tag, OnActionClickListener onActionClickListener) {
        SecretDoorPasswordDialogFragment secretDoorPasswordDialogFragment = new SecretDoorPasswordDialogFragment();
        secretDoorPasswordDialogFragment.setOnActionClickListener(onActionClickListener);
        secretDoorPasswordDialogFragment.show(manager, tag);
    }

    private AppCompatEditText mEtPassword;

    private OnActionClickListener mOnActionClickListener;

    public void setOnActionClickListener(OnActionClickListener onActionClickListener) {
        this.mOnActionClickListener = onActionClickListener;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_fragment_secret_door_password;
    }

    @Override
    protected void onLayoutInflated(View view) {
        setDialogCancelable(false);
        mEtPassword = view.findViewById(R.id.et_password);
        view.findViewById(R.id.bt_cancel).setOnClickListener(this);
        view.findViewById(R.id.bt_confirm).setOnClickListener(this);
    }

    @Override
    protected WindowManager.LayoutParams initLayoutParams(WindowManager.LayoutParams layoutParams) {
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width = (int) (ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(32));
        return layoutParams;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.bt_cancel) {
            dismiss();
        } else if (id == R.id.bt_confirm) {
            if (mOnActionClickListener != null) {
                String password = "";
                Editable text = mEtPassword.getText();
                if (text != null) {
                    password = text.toString();
                }
                boolean dismiss = mOnActionClickListener.onConfirmClick(password);
                if (dismiss) {
                    dismiss();
                }
            }
        }
    }

    public interface OnActionClickListener {
        boolean onConfirmClick(String password);
    }
}
